package com.thh.idempotent.controller;

import com.thh.idempotent.annotation.AutoIdempotent;
import com.thh.idempotent.constant.Constant;
import com.thh.idempotent.model.vo.ResultVo;
import com.thh.idempotent.service.TestService;
import com.thh.idempotent.util.JSONUtil;
import com.thh.idempotent.util.TokenUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class BusinessController {


    @Resource
    private TokenUtil tokenUtil;

    @Resource
    private TestService testService;


    @PostMapping("/get/token")
    public String  getToken(){
        String token = tokenUtil.createToken();
        if (!StringUtils.isEmpty(token)) {
            ResultVo resultVo = new ResultVo();
            resultVo.setCode(Constant.CODE_SUCCESS);
            resultVo.setMessage(Constant.MESSAGE_SUCCESS);
            resultVo.setData(token);
            return JSONUtil.toJsonStr(resultVo);
        }
        return "getToken fail";
    }


    @AutoIdempotent
    @PostMapping("/test/Idempotence")
    public String testIdempotence() {
        String businessResult = testService.testIdempotence();
        if (!StringUtils.isEmpty(businessResult)) {
            ResultVo successResult = ResultVo.getSuccessResult(businessResult);
            return JSONUtil.toJsonStr(successResult);
        }
        return " test Idempotence fail";
    }
}